import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Soldier } from '../entity/Soldier';
import { DevelopIndexItem } from './DevelopIndexItem';
import { UserInfo } from '../common/UserInfo';
import { EventManager } from '../event/EventManager';
import { UIHeader } from '../common/UIHeader';
import { Tech, TechType } from '../entity/Tech';
import { BManager } from '../entity/BManager';
import { buildingType } from '../settings/const';
const { ccclass, property } = _decorator;

@ccclass('DevelopIndex')
export class DevelopIndex extends Component {
    @property(UIHeader)
    uiHeader:UIHeader
    @property(Label)
    tittle:Label
    @property(Node)
    soldierLayout:Node;
    @property(Node)
    buildLayout:Node;
   
    @property(Prefab)
    indexItem:Prefab;

    async start() {
        let b = BManager.getBManageById(buildingType.B22_Academy)
        if(b){
            this.uiHeader.setData(`研究院 等级${b.grade}`)
            EventManager.instance.on(`研究院 等级${b.grade}close`, this.onClose, this)
        } else {
            this.uiHeader.setData('研究院')
            EventManager.instance.on('研究院close', this.onClose, this)
        }

       
        EventManager.instance.on(EventManager.EventType.DevelopComplete, this.renderData, this)
        EventManager.instance.on(EventManager.EventType.GetVillageDetail, this.renderData, this)
        await UserInfo.instance.refreshVillageDetail()
    }

    protected onDisable(): void {
        EventManager.instance.off(EventManager.EventType.DevelopComplete, this.renderData, this)
        EventManager.instance.off(EventManager.EventType.GetVillageDetail, this.renderData, this)
    }

    async renderData(){
        this.buildLayout.destroyAllChildren()
        this.soldierLayout.destroyAllChildren()
        let list = Tech.getList()
      
        console.log(list)

        for(let tech of list){
            let node = instantiate(this.indexItem)
            
            node.getComponent(DevelopIndexItem).setData(tech)

            if(tech.type == TechType.Building){
                this.buildLayout.addChild(node)
            } else {
                this.soldierLayout.addChild(node)
            }
        }
       
    }

   

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.DevelopIndex)
    }
}


